﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

public static class Utils
{
    public static string GetHostName(string url)
    {
        return new Uri(url).Host;
    }

    public static string GetScheme(string url)
    {
        return new Uri(url).Scheme;
    }

    public static int GetPort(string url)
    {
        return new Uri(url).Port;
    }

    public static string GetHost(string url)
    {
        string scheme = GetScheme(url);
        string hostName = GetHostName(url);
        string port = GetPort(url).ToString();
        string host = $"{scheme}://{hostName}:{port}";
        return host;
    }

    public static List<string> ReadTextFile(string inFile)
    {
        StreamReader sr = new StreamReader(inFile, Encoding.Default);
        string line = "";
        List<string> lines = new List<string>();
        while ((line = sr.ReadLine()) != null)
        {
            lines.Add(line);
        }
        return lines;
    }
    public static void WriteTextFile(string outFile, List<string> lines)
    {
        StreamWriter sw = new StreamWriter(outFile, false, Encoding.Default);
        foreach (string line in lines)
        {
            sw.WriteLine(line);
        }
        sw.Flush();
        sw.Close();
    }

    public static string Encrypt(string plainText, byte[] key, byte[] iv)
    {
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = key;
            aesAlg.IV = iv;
            aesAlg.Mode = CipherMode.CBC;
            aesAlg.Padding = PaddingMode.PKCS7;

            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            using (var msEncrypt = new System.IO.MemoryStream())
            {
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                using (var swEncrypt = new System.IO.StreamWriter(csEncrypt))
                {
                    swEncrypt.Write(plainText);
                }
                return Convert.ToBase64String(msEncrypt.ToArray());
            }
        }
    }

    public static string Decrypt(string cipherText, byte[] key, byte[] iv)
    {
        cipherText = cipherText.Replace("\n", "").Replace("\r", ""); // 去除换行符
        byte[] cipherBytes = Convert.FromBase64String(cipherText);

        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = key;
            aesAlg.IV = iv;
            aesAlg.Mode = CipherMode.CBC;
            aesAlg.Padding = PaddingMode.PKCS7;

            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            using (var msDecrypt = new System.IO.MemoryStream(cipherBytes))
            using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            using (var srDecrypt = new System.IO.StreamReader(csDecrypt))
            {
                return srDecrypt.ReadToEnd();
            }
        }
    }

    static System.Diagnostics.Process p;
    public static void RunExe(string exe, string args)
    {
        p = new System.Diagnostics.Process();
        p.StartInfo.FileName = exe;
        p.StartInfo.Arguments = args;
        p.Start();
    }

    public static void PlayFinishedFile()
    {
        SoundPlayer player = new SoundPlayer(HuaJuanAudioNevolDown.Properties.Resources.download_complete);

        // 加载音频文件
        player.Load();

        // 播放音频文件
        player.Play();

        // 注意：如果不希望等待播放结束，可以移除上面的循环
        // 程序会继续执行，而音频文件会在后台继续播放

        // 最后，确保资源被释放
        player.Dispose();
    }

    public static List<List<string>> SplitList(List<string> source, int chunkSize)
    {
        List<List<string>> result = new List<List<string>>();
        for (int i = 0; i < source.Count; i += chunkSize)
        {
            // 使用 GetRange 方法获取子列表
            List<string> chunk = source.GetRange(i, Math.Min(chunkSize, source.Count - i));
            result.Add(chunk);
        }
        return result;
    }
}
